package cn.gailvlun.gll.chatkit;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.blankj.utilcode.util.FileUtils;

import java.io.File;
import java.util.UUID;

import cn.gailvlun.gll.chatkit.record.IRecord;
import cn.gailvlun.gll.chatkit.record.MediaRecorderMgr;



public class RecordService extends Service {

    private IRecord mRecord;

    @Override
    public void onCreate() {
        super.onCreate();
        mRecord = MediaRecorderMgr.getInstance();
    }

    private DemoBinder mBinder = new DemoBinder();
    private File path = new File(Environment.getExternalStorageDirectory(), "gailvlun/audio/");
    private String currentFileName;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    private void startServiceRecord() {
        currentFileName = UUID.randomUUID() + ".amr";
        if (FileUtils.createOrExistsDir(path)) {
            mRecord.startRecord(new File(path, currentFileName).getPath());
        }
    }

    private void stopServiceRecord() {
        mRecord.stopRecord();
    }

    private void cancelServiceRecord() {
        mRecord.cancelRecord(new File(path, currentFileName).getPath());
    }

    
    private int getServiceVolumeRate() {
        return mRecord.getVolumeRate();
    }

    public class DemoBinder extends Binder {
        public void startRecord() {
            startServiceRecord();
        }

        public void stopRecord() {
            stopServiceRecord();
        }

        public void cancelRecord() {
            cancelServiceRecord();
        }

        public int getVolumeRate() {
            return getServiceVolumeRate();
        }

        public File getCurrentFile() {
            return new File(path, currentFileName);
        }
    }
}
